﻿// Copyright 2006 - Ricardo Stuven (rstuven@gmail.com)
//
// This file is part of MsSqlSpatial.
// MsSqlSpatial is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// MsSqlSpatial is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with MsSqlSpatial; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.ComponentModel;
using MsSqlSpatial;

public partial class StoredProcedures
{

	/// <summary>
	/// Update the SRID of all features in a geometry column updating constraints 
	/// and reference in GEOMETRY_COLUMNS.
	/// </summary>
	/// <param name="schema_name"></param>
	/// <param name="table_name"></param>
	/// <param name="column_name"></param>
	/// <param name="srid"></param>
	[SqlProcedure]
	public static void UpdateGeometrySRID(
		SqlString schema_name,
		SqlString table_name,
		SqlString column_name,
		SqlInt32 srid)
	{
		using (SqlConnection connection = DatabaseHelper.ContextConnection)
		{
			connection.Open();
			SqlTransaction transaction = connection.BeginTransaction();
			try
			{
				using (SqlCommand command = connection.CreateCommand())
				{
					command.Transaction = transaction;
					string schemaName = DatabaseHelper.CheckSchemaName(command, schema_name);
					string tableName = table_name.Value;
					string columnName = column_name.Value;
					int sridValue = srid.Value;
	
					if (!SpatialRefSysTable.CheckSRIDExists(command, sridValue))
					{
						throw new InvalidSridException(sridValue);
					}
	
					if (!GeometryColumnsTable.CheckGeometryColumnExists(command, schemaName, tableName, columnName))
					{
						throw new ApplicationException("Column not found in [ST].[GEOMETRY_COLUMNS] table.");
					}
	
					DatabaseHelper.BuildCommand(command, string.Format(
@"UPDATE [ST].[GEOMETRY_COLUMNS]
SET [SRID] = {0}
WHERE [F_TABLE_SCHEMA] = @p0 
AND [F_TABLE_NAME] = @p1
AND [F_GEOMETRY_COLUMN] = @p2"
, sridValue)
, schemaName
, tableName
, columnName
);
					command.ExecuteNonQuery();
	
					DatabaseHelper.BuildCommand(command, string.Format(
@"ALTER TABLE [{0}].[{1}]
DROP CONSTRAINT [enforce_srid_{2}]"
, schemaName, tableName, columnName));
					command.ExecuteNonQuery();
	
					DatabaseHelper.BuildCommand(command, string.Format(
@"UPDATE [{0}].[{1}]
SET [{2}] = [ST].[SetSRID]([{2}], {3})"
, schemaName, tableName, columnName, sridValue));
					command.ExecuteNonQuery();
	
					
					GeometryTable.ExecuteAddConstraintSRID(command, schemaName, tableName, columnName, sridValue);
	
					transaction.Commit();
					SqlContext.Pipe.Send(string.Format(
@"Column SRID [{0}].[{1}].[{2}] changed to {3}."
, schemaName, tableName, columnName, sridValue));
				}
			}
			catch
			{
				transaction.Rollback();
				throw;
			}
		}

	}
};
